From 4268a1ddc0b08a83eec3c0356bd42c4a39830dd3 Mon Sep 17 00:00:00 2001 From: tsteven4 Date: Wed, 4 Jul 2018 14:20:00 -0600 Subject: [PATCH] miscellaneous cleanups & bug fixes. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit easygps.cc: bugfix - clang-tidy check misc-sizeof-expression garmin_fs.cc: warning cleanup - gcc warning: warning: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 3 [-Wformat-truncation=] jtr.cc: warning cleanup - gcc warning: ‘__builtin___snprintf_chk’ output may be truncated before the last format character [-Wformat-truncation=] jtr.cc: bugfix - the jtr writter could incorrecly print the msec portion of the creationtime by dropping the most significant digit when it was zero. mapsource.cc: warning cleanup - binary operator acts on identical operands random.cc: bugfix - fractional seconds were always zero. tpo.cc: warning cleanup - gcc warning: argument 2 null where non-null expected [-Wnonnull] xmlgeneric.cc: warning cleanup - Declarator is never used --- easygps.cc | 2 +- garmin_fs.cc | 4 ++++ jtr.cc | 36 +++++++++++++++++++----------------- mapsource.cc | 12 ++++++------ random.cc | 2 +- tpo.cc | 5 +++++ xmlgeneric.cc | 14 +++++--------- 7 files changed, 41 insertions(+), 34 deletions(-) diff --git a/easygps.cc b/easygps.cc index 0fbb19226..616d6c602 100644 --- a/easygps.cc +++ b/easygps.cc @@ -48,7 +48,7 @@ rd_init(const QString& fname) sz = gbfread(ibuf, 1, 52, file_in); if ((sz < 52) || - strncmp(ibuf, ezsig, sizeof(ezsig)-1) || + strncmp(ibuf, ezsig, strlen(ezsig)) || (ibuf[51] != 'W')) { fatal(MYNAME ": %s is not an EasyGPS file.\n", qPrintable(fname)); } diff --git a/garmin_fs.cc b/garmin_fs.cc index ef292dcb5..bdef0361e 100644 --- a/garmin_fs.cc +++ b/garmin_fs.cc @@ -27,6 +27,7 @@ #include "inifile.h" #include +#include #include #include @@ -388,6 +389,9 @@ garmin_fs_convert_category(const char* category_name, uint16_t* category) char* c; char key[3]; + // use assertion to silence gcc 7.3 warning + // warning: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 3 [-Wformat-truncation=] + assert((i>=0) && (i<16)); snprintf(key, sizeof(key), "%d", i + 1); c = inifile_readstr(global_opts.inifile, GMSD_SECTION_CATEGORIES, key); if ((c != nullptr) && (case_ignore_strcmp(c, category_name) == 0)) { diff --git a/jtr.cc b/jtr.cc index f4b7ad910..d85ede24d 100644 --- a/jtr.cc +++ b/jtr.cc @@ -22,6 +22,7 @@ #include "defs.h" #include "csv_util.h" #include +//#include #include #include #include @@ -265,23 +266,24 @@ static void jtr_trkpt_disp_cb(const Waypoint* wpt) { char* str, *tmp; - char stime[10], sdate[7], scourse[6], sspeed[8]; - struct tm tm; + char scourse[6], sspeed[8]; + QString sdate; + QString stime; if (wpt->creation_time.isValid()) { - const time_t tt = wpt->GetCreationTime().toTime_t(); - tm = *gmtime(&tt); - - tm.tm_year += 1900; - tm.tm_mon += 1; - snprintf(sdate, sizeof(sdate), "%02d%02d%02d", tm.tm_mday, tm.tm_mon, tm.tm_year % 100); - snprintf(stime, sizeof(stime), "%02d%02d%02d.%02d", tm.tm_hour, tm.tm_min, tm.tm_sec, wpt->creation_time.time().msec()); - if (wpt->creation_time.time().msec() == 0) { - stime[6] = 0; - } - } else { - stime[0] = 0; - sdate[0] = 0; + gpsbabel::DateTime dt = wpt->GetCreationTime().toUTC(); + + // round time to centiseconds. + int msec = dt.time().msec(); + int csec = lround(msec/10.0); + dt = dt.addMSecs(csec*10-msec); + sdate = dt.toString(QStringLiteral("ddMMyy")); + stime = dt.toString(QStringLiteral("HHmmss.zzz")); + // toss milliseconds position which should now be zero. + //assert(stime.endsWith('0')); + stime.chop(1); + // suppress fractional seconds if they are zero. + stime = stime.replace(QLatin1String(".00"), QLatin1String("")); } if (WAYPT_HAS(wpt, speed) && (wpt->speed >= 0)) { snprintf(sspeed, sizeof(sspeed), "%.1f", MPS_TO_KNOTS(wpt->speed)); @@ -295,7 +297,7 @@ jtr_trkpt_disp_cb(const Waypoint* wpt) } xasprintf(&str, "GEOTAG2,%s,%c,%09.4f,%c,%010.4f,%c,%s,%s,%s,,E,A", - stime, + CSTR(stime), wpt->creation_time.isValid() ? 'A' : 'V', fabs(degrees2ddmm(wpt->latitude)), wpt->latitude < 0 ? 'S' : 'N', @@ -303,7 +305,7 @@ jtr_trkpt_disp_cb(const Waypoint* wpt) wpt->longitude < 0 ? 'W' : 'E', sspeed, scourse, - sdate); + CSTR(sdate)); xasprintf(&tmp, "%s*%02X", str, nmea_cksum(str)); xfree(str); diff --git a/mapsource.cc b/mapsource.cc index 916046cad..17a3e59c1 100644 --- a/mapsource.cc +++ b/mapsource.cc @@ -609,7 +609,7 @@ mps_waypoint_r(gbfile* mps_file, int mps_ver, Waypoint** wpt, unsigned int* mpsc * MRCB */ static void -mps_waypoint_w(gbfile* mps_file, int mps_ver, const Waypoint* wpt, const int isRouteWpt) +mps_waypoint_w(gbfile* mps_file, int mps_ver, const Waypoint* wpt, const bool isRouteWpt) { int reclen; int lat, lon; @@ -768,7 +768,7 @@ mps_waypoint_w_unique_wrapper(const Waypoint* wpt) /* if this waypoint hasn't been written then it is okay to do so */ if (wptfound == nullptr) { - mps_waypoint_w(mps_file_out, mps_ver_out, wpt, (1==0)); + mps_waypoint_w(mps_file_out, mps_ver_out, wpt, false); /* ensure we record in our "private" queue what has been written so that we don't write it again */ @@ -807,10 +807,10 @@ mps_route_wpt_w_unique_wrapper(const Waypoint* wpt) if (wptfound == nullptr) { /* well, we tried to find: it wasn't written and isn't a real waypoint */ - mps_waypoint_w(mps_file_out, mps_ver_out, wpt, (1==1)); + mps_waypoint_w(mps_file_out, mps_ver_out, wpt, true); mps_wpt_q_add(&written_route_wpt_head, wpt); } else { - mps_waypoint_w(mps_file_out, mps_ver_out, wpt, (1==0)); + mps_waypoint_w(mps_file_out, mps_ver_out, wpt, false); /* Simulated real user waypoint */ mps_wpt_q_add(&written_wpt_head, wpt); } @@ -849,11 +849,11 @@ mps_waypoint_w_uniqloc_wrapper(Waypoint* wpt) wptfound = new Waypoint(*wpt); xfree(wptfound->shortname); wptfound->shortname = newName; - mps_waypoint_w(mps_file_out, mps_ver_out, wptfound, (1==0)); + mps_waypoint_w(mps_file_out, mps_ver_out, wptfound, false); mps_wpt_q_add(&written_wpt_head, wpt); } } else { - mps_waypoint_w(mps_file_out, mps_ver_out, wpt, (1==0)); + mps_waypoint_w(mps_file_out, mps_ver_out, wpt, false); /* ensure we record in out "private" queue what has been written so that we don't write it again */ mps_wpt_q_add(&written_wpt_head, wpt); diff --git a/random.cc b/random.cc index 89631f4dc..d30fde92f 100644 --- a/random.cc +++ b/random.cc @@ -183,7 +183,7 @@ random_read() wpt->SetCreationTime(time); if RND(3) { - wpt->creation_time = wpt->creation_time.addMSecs(rand_int(1000) * 1000); + wpt->creation_time = wpt->creation_time.addMSecs(rand_int(1000)); } time += rand_int(10) + 1; diff --git a/tpo.cc b/tpo.cc index d1036e296..7acd43b95 100644 --- a/tpo.cc +++ b/tpo.cc @@ -74,6 +74,7 @@ #include "defs.h" #include "jeeps/gpsmath.h" /* for datum conversions */ #include // Wish we could use c++11... +#include #include #include #include @@ -1453,6 +1454,10 @@ tpo_read() static void tpo_write_file_header() { + // this assertion will quiet gcc 7.3 warnings about output_state in the strncmp calls + // warning: argument 2 null where non-null expected [-Wnonnull] + assert(output_state != nullptr); + /* force upper-case state name */ strupper(output_state); diff --git a/xmlgeneric.cc b/xmlgeneric.cc index aee85d1d2..3b61370d3 100644 --- a/xmlgeneric.cc +++ b/xmlgeneric.cc @@ -35,7 +35,6 @@ #include #endif -static QString current_tag; static xg_tag_mapping* xg_tag_tbl; static QSet xg_ignore_taglist; @@ -100,9 +99,10 @@ xml_consider_ignoring(const QStringRef& name) } static void -xml_run_parser(QXmlStreamReader& reader, QString& current_tag) +xml_run_parser(QXmlStreamReader& reader) { xg_callback* cb; + QString current_tag; while (!reader.atEnd()) { switch (reader.tokenType()) { @@ -175,13 +175,12 @@ readnext: void xml_read() { gpsbabel::File file(rd_fname); - QString current_tag; file.open(QIODevice::ReadOnly); QXmlStreamReader reader(&file); - xml_run_parser(reader, current_tag); + xml_run_parser(reader); if (reader.hasError()) { fatal(MYNAME ":Read error: %s (%s, line %ld, col %ld)\n", qPrintable(reader.errorString()), @@ -209,13 +208,11 @@ void xml_readprefixstring(const char* str) // determine file encoding, falls back to UTF-8 if unspecified. void xml_readstring(const char* str) { - QString current_tag; - reader_data.append(str); QXmlStreamReader reader(reader_data); - xml_run_parser(reader, current_tag); + xml_run_parser(reader); if (reader.hasError()) { fatal(MYNAME ":Read error: %s (%s, line %ld, col %ld)\n", qPrintable(reader.errorString()), @@ -229,10 +226,9 @@ void xml_readstring(const char* str) // encoding because the source is already Qt's internal UTF-16. void xml_readunicode(const QString& str) { - QString current_tag; QXmlStreamReader reader(str); - xml_run_parser(reader, current_tag); + xml_run_parser(reader); } /******************************************/ -- 2.30.2